﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCE.UI.Blocks;
using Baosight.ZNH.ZMCE.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls.GridView;

namespace Baosight.ZNH.ZMCE.UI
{
    /// <summary>
    /// 设备台账
    /// </summary>
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张志芳
    /// Version: 1.0
    /// History:
    ///       2011-07-08  张志芳   [创建]
    ///
    public partial class ZMCEF1 : EFViewCmes
    {
        private Dictionary<object, string> TabToPageName; //存放页签与页面号的对应关系
        public ZMCEF1()
        {
            InitializeComponent();
            TabToPageName = new Dictionary<object, string>();
            TabToPageName.Add(tab1, "ZMCEF2");
            TabToPageName.Add(tab2, "ZMCEF3");
            TabToPageName.Add(tab3, "ZMCEF4");
            TabToPageName.Add(tab4, "ZMCEF5");
            TabToPageName.Add(tab5, "ZMCEF6");
            TabToPageName.Add(tab6, "ZMCEF7");
            TabToPageName.Add(tab7, "ZMCEF8");
            TabToPageName.Add(tab8, "ZMCEF9");
            TabToPageName.Add(tab9, "ZMCEFA");
            TabToPageName.Add(tab13, "ZMCEFE");
            TabToPageName.Add(tab14, "ZMCEFF");
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        public override void Initialize(EIInfo initData)
        {
            EquipmentCardBlock block = new EquipmentCardBlock();
            initData.AddBlock(block.getIntactType("cmes.ce.intactType"));
            initData.AddBlock(block.getStatus("cmes.ce.status"));
            base.Initialize(initData);
        }

        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (this.grid.SelectedItem != null)
            {
                LoadTab(this.grid.SelectedItem, this.tabPage.SelectedItem);
            }
            else
            {
                (this.tabPage.SelectedItem as EFTabItem).Content = null;
            }
        }

        /// <summary>
        /// 页签改变加载不同的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {
            EFTab tab = sender as EFTab;
            if (this.grid.SelectedItem != null)
            {
                LoadTab(this.grid.SelectedItem, tab.SelectedItem);
            }
        }

        /// <summary>
        /// 加载不同的页签
        /// </summary>
        /// <param name="gridItem">列表中的一行数据</param>
        /// <param name="tabItem">EFTab控件对象</param>
        private void LoadTab(object gridItem, object tabItem)
        {
            var fdKeyId = gridItem.GetType().GetProperty("fdKeyId").GetValue(gridItem, null);
            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("fdEquipmentId", fdKeyId.ToString());
            args.Add("fdStandardsForm", "1");
            if (tabItem != null)
            {
                (tabItem as EFTabItem).Content = null;
                (tabItem as EFTabItem).LoadView(TabToPageName[tabItem], args);
            }
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.status"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.intactType"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cp.engineering"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.department"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipmentSource"));
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.ce.equipmentClass"));
            _eIInfo.AddBlock(new EquipmentCardBlock().getIsFiling("cmes.ce.isFiling"));
            EquipmentCard view = new EquipmentCard(_dialogType);//构造一个新的修改页面
            view.EIDataContext = new Platform.Core.UI.EIDataContext();
            view.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = view;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;

            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "设备-查看", "detail", null, sender, e);
        }

    }
}
